﻿using System;
using System.Collections;
using mojoPortal.Business;
using mojoPortal.Web.Framework;
using mojoPortal.Web;
using Resources;
using ud_Article.Business;

namespace ud_Article.UI
{
    public partial class SearchResultPage : mojoBasePage
    {
        protected int pageId = -1;
        protected int moduleId = -1;
        private int searchCategoryId = -1;
        private int searchModuleId = -1;
        private int searchTypeId = -1;
        private string searchTerm = string.Empty;
        private bool userCanEdit;
        private int pageNumber = 1;
        private ArticleSearchConfiguration config = new ArticleSearchConfiguration();

        #region OnInit
        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
        }

        override protected void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            Load += Page_Load;
            base.OnInit(e);
        }
        #endregion

        private void LoadPanels()
        {
            LoadSideContent(config.ShowLeftPanelSetting, config.ShowRightPanelSetting, config.ShowTopPanelSetting, config.ShowBottomPanelSetting, config.ContentPanelSetting);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!UserCanViewPage(moduleId))
            {
                SiteUtils.RedirectToAccessDeniedPage();
                return;
            }

            LoadSettings();
            PopulateControls();

        }

        private void PopulateControls()
        {
            Title = SiteUtils.FormatPageTitle(siteSettings, ArticleResources.PostList);
            moduleTitle.ModuleInstance = GetModule(moduleId);
            
            postList.ModuleId = moduleId;
            postList.PageId = pageId;
            postList.IsEditable = userCanEdit;
            postList.SiteRoot = SiteRoot;
            postList.ImageSiteRoot = ImageSiteRoot;
            postList.Config = config;
            postList.SearchCategoryId = searchCategoryId;
            postList.SearchModuleId = searchModuleId;
            postList.SearchTerm = searchTerm;
            postList.SearchTypeId = searchTypeId;
            //make this page look as close as possible to the way a cms page with the blog module on it looks
        }

        private void LoadSettings()
        {
            userCanEdit = UserCanEditModule(moduleId);
            if (userCanEdit) { Article.CountOfDrafts(moduleId); }
            pnlWrapper.CssClass += " " + config.ResultCssSetting;
            //pnl1.CssClass += " " + config.InstanceCssClass;
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", moduleId);
            pageNumber = WebUtils.ParseInt32FromQueryString("pagenumber", pageNumber);
            searchModuleId = WebUtils.ParseInt32FromQueryString("sm", searchModuleId);
            searchCategoryId = WebUtils.ParseInt32FromQueryString("sc", searchCategoryId);
            searchTypeId = WebUtils.ParseInt32FromQueryString("st", searchTypeId);
            searchTerm = Session["searchterm"] == null ? string.Empty : Session["searchterm"].ToString();
            Session["searchterm"] = searchTerm;
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            config = new ArticleSearchConfiguration(settings);
        }
    }
}